package com.wwcom.darteagle;

import com.wwcom.darteagle.util.Tips;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;

public class OptionsActivity extends PreferenceActivity {
	private String lastTheme;
	private boolean resumeMatch;
	
    /** Called when the activity is first created. */	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        resumeMatch= false;
        Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
        	resumeMatch= extras.getBoolean(HomeActivity.RESUME_MATCH);
        }        
        
        addPreferencesFromResource(R.xml.preferences);
        ListPreference themePref= (ListPreference) findPreference("themePref");
        CharSequence []themes= new CharSequence[3];
		CharSequence[] themeVals = new CharSequence[3];
		themes[0] = "PDC (SKY)";
		themes[1] = "PDC World Champs 2012 (SKY)";
		themes[2] = "BDO (BBC)";
		themeVals[0] = "PDC";
		themeVals[1] = "PDC.WC2012";
		themeVals[2] = "BDO";
		themePref.setEntries(themes);
		themePref.setEntryValues(themeVals);
		
		//remember last theme
    	lastTheme= getThemeName();
		
        // Clear Twitter authentication custom preference
  	    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        Preference customPref = (Preference) findPreference("clearTwitterPref");
        customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
        	  Toast.makeText(getBaseContext(), getString(R.string.options_twitter_cleared),
        			  Toast.LENGTH_LONG).show();
        	  SharedPreferences.Editor editor= prefs.edit();
        	  editor.putString("token", null);
        	  editor.putString("tokenSecret", null);
        	  editor.commit();
        	  return true;
          }

        });        

        // Reset tips custom preference
        Preference tipsPref = (Preference) findPreference("resetTips");
        tipsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
        	  Toast.makeText(getBaseContext(),getString(R.string.options_all_tips_shown),
        			  Toast.LENGTH_LONG).show();
        	  SharedPreferences.Editor editor= prefs.edit();
        	  editor.putBoolean(Tips.EDITSCORE, true);
        	  editor.putBoolean(Tips.MATCHSTATS, true);
        	  editor.commit();
        	  return true;
          }

        });        
        
        
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	
    	String newTheme= getThemeName();
    	//restart application to ensure theme gets changed
    	if (!lastTheme.equals(newTheme)) {  
    		Intent i = new Intent(this,HomeActivity.class);
    		i.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    		i.putExtra(HomeActivity.RESUME_MATCH, resumeMatch);
    		startActivity(i);    		
    	}
    }
    
       private String getThemeName() {
     	    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);  	    
      	    String newTheme= prefs.getString("themePref", "PDC");
      	    //String fontSize= prefs.getString("fontSizePref", "Normal");
      	    //if (!fontSize.equals("Normal")) {
      	    //	newTheme= newTheme + "." + fontSize;
      	    //}
      	    return newTheme;
       }
    
}